﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ParseData
{
    public class ECGSensorReading : ISensorReading
    {
        private ECGSensor sensor;

        private string _Date = "";
        /// <summary>Get or set the reading date.</summary>
        public string Date
        {
            get { return this._Date; }
            set { this._Date = value; }
        }

        private string _Hour = "";
        /// <summary>Get or set the reading hour.</summary>
        public string Hour
        {
            get { return this._Hour; }
            set { this._Hour = value; }
        }

        private double _ValuePQ;
        /// <summary>Get or set the reading PQ interval value.</summary>
        public double ValuePQ
        {
            get { return this._ValuePQ; }
            set { this._ValuePQ = value; }
        }

        private double _ValueHeart;
        /// <summary>Get or set the reading heart rate value.</summary>
        public double ValueHeart
        {
            get { return this._ValueHeart; }
            set { this._ValueHeart = value; }
        }

        /// <summary>Constructor.</summary>
        public ECGSensorReading(ECGSensor sensor)
        {
            this.sensor = sensor;
        }

        /// <summary>Constructor + value.</summary>
        public ECGSensorReading(ECGSensor sensor, double valuePQ, double valueHeart)
        {
            this.sensor = sensor;
            this.ValuePQ = valuePQ;
            this.ValueHeart = valueHeart;
        }

        public bool HasValue()
        {
            bool hasVal = false;
            if (_ValuePQ > 0) hasVal = true;
            return hasVal;
        }

        /// <summary>Return values format.</summary>
        public string ReadableString
        {
            get
            {
                //String result = String.Format("PQ:{0}, Heart:{1}", _ValuePQ, _ValueHeart);
                String result = _ValuePQ.ToString("0.00") + " " + _ValueHeart.ToString("000.0");
                if (_Date.Length != 0)
                    result += String.Format(" {0}", _Date);
                if (_Hour.Length != 0)
                    result += String.Format(" {0}", _Hour);
                return result;
            }
        }
    }
    /// <summary>
    /// Class to read data files with ECG values of the desired type.
    /// </summary>
    public class ECGSensor : ParseData.ISensor
    {
        private string[] fileLines;
        private string _SensorID;

        private double sumPQ = 0;
        private double sumHeart = 0;
        private int countPQ = 0;
        private int countHeart = 0;

        public ISensorReading AverageValue
        {
            get
            {
                if (countPQ > 0 && countHeart > 0)
                    return new ECGSensorReading(this, sumPQ / countPQ, sumHeart / countHeart);
                else
                    return null;
            }
        }

        private double minPQ = 0;
        private double minHeart = 0;
        public ISensorReading MinValue
        {
            get
            {
                if (countPQ > 0 && countHeart > 0)
                    return new ECGSensorReading(this, minPQ, minHeart);
                else
                    return null;
            }
        }

        private double maxPQ = 0;
        private double maxHeart = 0;
        public ISensorReading MaxValue
        {
            get
            {
                if (countPQ > 0 && countHeart > 0)
                    return new ECGSensorReading(this, maxPQ, maxHeart);
                else
                    return null;
            }
        }

        public void ResetMinValue()
        {
            minPQ = 0;
            minHeart = 0;
        }
        public void ResetMaxValue()
        {
            maxPQ = 0;
            maxHeart = 0;
        }

        ECGSensorReading lastReading;

        public string SenzorID
        {
            get { return this._SensorID; }
            set { this._SensorID = value; }
        }

        public string TypeName
        {
            get { return "ECG sensor"; }
        }

       /// <param name="FileName">Location of the file.</param>
        public ECGSensor(string Filename)
        {
            string fileLine = "";
            fileLines = FileReaderUtil.SplitFileByLine(Filename);

            // Sensor's ID
            int indexID = 1;
            {
                fileLine = fileLines[indexID].Trim();
                _SensorID = fileLine.Substring(0, 6).Trim();
            }
        }

        private int fileIndex = 3;

        /// <summary>
        /// Parse the given positional file.
        /// </summary>
        /// <param name="FileName">Location of the file.</param>
        public ISensorReading ReadNewValue()
        {
         if (fileIndex < fileLines.Length)
        {
            string fileLine = fileLines[fileIndex].Trim();
            lastReading = new ECGSensorReading(this);

            if (fileLine.Length > 0)
            {
                lastReading.ValuePQ = Double.Parse(fileLine.Substring(0, 5).Trim(), new System.Globalization.CultureInfo("en-US"));
                lastReading.ValueHeart = Double.Parse(fileLine.Substring(5, 6).Trim(), new System.Globalization.CultureInfo("en-US"));
                lastReading.Date = fileLine.Substring(11, 11).Trim();
                lastReading.Hour = fileLine.Substring(22, 8).Trim();
            }

            double curentSenzorValPQ = lastReading.ValuePQ;
            double curentSenzorValHeart = lastReading.ValueHeart;

            //ECG caluclations
            //PQ interval calculations
            // min PQ       
            if (curentSenzorValPQ < 0.12) minPQ = curentSenzorValPQ;
            // max PQ  
            if (curentSenzorValPQ > 0.20) maxPQ = curentSenzorValPQ;
            // average PQ
            sumPQ = sumPQ + curentSenzorValPQ;
            countPQ++;
            double tempMed = sumPQ / countPQ;

            //P wave rate, Heart rate calculations
            // min Hart rate       
            if (curentSenzorValHeart < 60) minHeart = curentSenzorValHeart;
            // max Hart rate   
            if (curentSenzorValHeart > 100) maxHeart = curentSenzorValHeart;
            // average Hart rate  
            sumHeart = sumHeart + curentSenzorValHeart;
            countHeart++;
            double tempMed1 = sumHeart / countHeart;

            fileIndex++;
             }
             else
             {
                 lastReading = null;
             }

             return lastReading;

        }
    }
}
